<?php
require_once 'jujucock.php';

define('PORT', 54344);

$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$ok = socket_connect($sock, '127.0.0.1', PORT);
if (!$ok) {
    $errorstr = sprintf("Error Creating Socket -- [%d] %s", socket_last_error(), socket_strerror(socket_last_error()));
    printf("%s\n", $errorstr);
    socket_close($sock);
    exit(1);
}

$times = 10;

$channel = new JuTCPChannel($sock);
$request = "Hello,World.";

$packet = new Packet();
$packet->setRequest();
$packet->service = 'ECHO';
$packet->body = join('', array($request, str_repeat('A', 32)));

for ($i=0; $i<$times; $i++) {
    list($record, $packid) = enpack($packet);
    $channel->send($record . "\n");
    printf("send #%d (packid:%d) %s\n", $i+1, $packid, $record);
}

sleep(1);

socket_close($sock); 